<?php
function err(){
     header("HTTP/1.1 404 Not Found");
     exit();
}
$urls=array("/home/repo/test"=>"https://chuangxiangwang:1234567a@gitee.com/chuangxiangnet/test.git master",
"/home/repo/coding/test"=>"https://wilddragon:123456a@git.coding.net/u/wilddragon/p/test.git master",
"/home/repo/android"=>"https://chuangxiangwang:1234567a@gitee.com/chuangxiangnet/android.git master",
"/home/repo/ios"=>"https://chuangxiangwang:1234567a@gitee.com/chuangxiangnet/ios.git master",
"/home/repo/web"=>"https://chuangxiangwang:1234567a@gitee.com/chuangxiangnet/web.git master",
"/home/repo/live-web"=>"https://chuangxiangwang:1234567a@gitee.com/chuangxiangnet/live-web.git master",
"/home/repo/share-view"=>"https://chuangxiangwang:1234567a@gitee.com/chuangxiangnet/share-view.git master",
"/home/repo/ChuangXiangHuiYiShiYuDingXiaoChengXu"=>"https://chuangxiangwang:1234567a@gitee.com/chuangxiangnet/ChuangXiangHuiYiShiYuDingXiaoChengXu.git master",
"/home/repo/ChuangXiangJingYingFanWeiXiaoZhuShou"=>"https://chuangxiangwang:1234567a@gitee.com/chuangxiangnet/ChuangXiangJingYingFanWeiXiaoZhuShou.git master",
"/home/repo/ERP"=>"https://chuangxiangwang:1234567a@gitee.com/houdalu/ERP.git master",
"/home/repo/coding/chuangxiang"=>"https://wilddragon:123456a@git.coding.net/u/hyd446107014/p/chuangxiang.git master",
"/home/repo/coding/companyRang"=>"https://wilddragon:123456a@git.coding.net/u/hoululu945/p/companyRang.git master"
);
if (($_SERVER['HTTP_USER_AGENT']=='Coding.net Hook')||($_SERVER['HTTP_USER_AGENT']=='git-oschina-hook')){
  $obj = json_decode(file_get_contents('php://input'));
  if (empty($obj->password)&&empty($obj->token)){
    err(); 
  }
  if ((!empty($obj->password))&&($obj->password!='token_coding_crejoy_20180103')){
    err(); 
  }
  if ((!empty($obj->token))&&($obj->token!='token_coding_crejoy_20180103')){
    err(); 
  }
  file_put_contents('/tmp/a.log',print_r($obj,true),FILE_APPEND); 
  $sync = false; 
  if (!empty($obj->commits)){
     foreach($obj->commits as $val){
       $msg = empty($val->message)?$val->short_message:$val->message;
       if (stristr($msg,'bug#')||stristr($msg,'story#')||stristr($msg,'task#')){
          $sync = true;
          $name = empty($obj->project)?$obj->repository->name:$obj->project->name;
          file_put_contents('/tmp/a.log',"\r\nname=$name\r\n",FILE_APPEND);
          $repo_dir = ($name=='test')?(empty($obj->project)?"/home/repo/coding/$name":"/home/repo/$name"):"/home/repo/$name";
          file_put_contents('/tmp/a.log',"\r\ndir=$repo_dir\r\n",FILE_APPEND);
          chdir($repo_dir);
          exec("/usr/bin/git config core.quotepath false");
          $cmd = "/usr/bin/git log -5";
          file_put_contents('/tmp/a.log',"\r\n1------>",FILE_APPEND);
          file_put_contents('/tmp/a.log',`$cmd`,FILE_APPEND);
          file_put_contents('/tmp/a.log',"<------1\r\n",FILE_APPEND);
          $cmd = "/usr/bin/git pull ".$urls[$repo_dir];
          $ret = exec($cmd,$arr,$r);
          file_put_contents('/tmp/a.log',"\r\n2------>",FILE_APPEND);
          file_put_contents('/tmp/a.log',"ret=$ret,val=$r,".print_r($arr,true),FILE_APPEND);
          file_put_contents('/tmp/a.log',"<------2\r\n",FILE_APPEND);
          break; 
       }
       else{
         file_put_contents('/tmp/a.log', "\r\nmessage:$msg\r\n", FILE_APPEND);
       }
     }
  }
  if ($sync){
      $cmd = "/bin/bash /opt/zbox/app/zentao/bin/syncgit.sh";
      //exec($cmd);
      file_put_contents('/tmp/a.log',`$cmd`,FILE_APPEND);
  }
  else{
      file_put_contents('/tmp/a.log', "\r\nno need sync\r\n", FILE_APPEND);
  }
}
else{
  err(); 
}
?>
